<?php
/**
 * created by zzy
 * date: 2018/1/16 10:48
 */
namespace App\Eloquent\Ygt;

class Address extends DbEloquent{

    protected $table    = 'ygt_address';

    //根据外键取列表(为什么是外键因为我还不知道外部要关联什么表)
    public static function getListByForeignKey($foreignKey)
    {
        $where          = ['foreign_key'=>$foreignKey];
        $result         = self::getList($where);
        return $result;
    }
    //取厂的客户地址列表
    public static function getListCompanyId($company_id)
    {
        $where          = ['company_id'=>$company_id];
        $result         = self::getList($where);
        return $result;
    }
    //添加或修改一个地址
    //$data = [
    //  'id'=>'0或不填为添加,有值为编辑修改','is_default'=>'1默认0非默认暂时未0可能用不到'
    //  'foreign_key'=>'外键id','company_id'=0,'province_id'=>0,'city_id'=>0,'area_id'=>0,'phone'=>''
    //  'consignee'=>'','province_name'=>0,'city_name'=>0,'area_name'=>0,'address'=>0,
    //];
    public static function putInData($data,$pk='')
    {
        $id             = isset($data['id']) ? intval($data['id']) : 0;
        $foreignKey     = isset($data['foreign_key']) ? intval($data['foreign_key']) : 0;
        $companyId      = isset($data['company_id']) ? intval($data['company_id']) : 0;
        $isDefault      = isset($data['is_default']) ? intval($data['is_default']) : 0;
        if($foreignKey == 0 || $companyId==0)
        {
            return false;
        }
        if($isDefault == 1)
        {
            //self::where('foreign_key',$foreignKey)->update(['is_default'=>0]);
        }
        $addressData        = [
            'is_default'=>$isDefault, 'foreign_key'=>$foreignKey, 'consignee'=>$data['consignee'],
            'company_id'=>$companyId,'phone'=>$data['phone'],
            'province_id'=>$data['province_id'],'city_id'=>$data['city_id'],'area_id'=>$data['area_id'],
            'province_name'=>$data['province_name'],'city_name'=>$data['city_name'],'area_name'=>$data['area_name'],
            'address'=>$data['address'],
        ];
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where, $addressData,$pk);
        } else {
            $result         = self::insertOneData($addressData,$pk);
        }
        return $result;
    }

}
